package cn.com.surker.production.service.impl;

import cn.com.surker.base.pump.dao.PumpNormDao;
import cn.com.surker.base.pump.dao.PumpTypeDao;
import cn.com.surker.base.pump.entity.PumpNorm;
import cn.com.surker.base.pump.entity.PumpType;
import cn.com.surker.common.MessageCenterException;
import cn.com.surker.enums.mes.PriceTypeEnum;
import cn.com.surker.enums.production.ProductionContractTypeEnum;
import cn.com.surker.enums.system.BillPriceTypeEnum;
import cn.com.surker.excel.ExcelReportUtils;
import cn.com.surker.oa.dao.OaProductionContractDao;
import cn.com.surker.oa.dao.OaSupplierDao;
import cn.com.surker.oa.entity.OaProductionContract;
import cn.com.surker.oa.entity.OaSupplier;
import cn.com.surker.production.dao.ProductionPumpBillDao;
import cn.com.surker.production.entity.ProductionPumpBill;
import cn.com.surker.production.service.PumpBillService;
import cn.com.surker.production.util.ExcelProductionReportUtils;
import cn.com.surker.production.vo.PumpBillVo;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.dao.SysOriginDao;
import cn.com.surker.system.entity.SysOrigin;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author lucky
 * @create 2025年05月23 16:46
 * @desc
 */
@Service
public class PumpBillServiceImpl implements PumpBillService {

    @Resource
    private ProductionPumpBillDao pumpBillDao;
    @Resource
    private OaProductionContractDao productionContractDao;
    @Resource
    private SysOriginDao originDao;
    @Resource
    private PumpNormDao pumpNormDao;
    @Resource
    private PumpTypeDao pumpTypeDao;
    @Resource
    private OaSupplierDao supplierDao;

    /**
     * 分页查询泵送结算
     * @param billVo
     * @param offset
     * @param limit
     * @return
     */
    public IPage findByPage(PumpBillVo billVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        QueryWrapper<ProductionPumpBill> billQuery = new QueryWrapper<>();
        if (StringUtils.isNull(billVo.getStatus()))
            billVo.setStatus(0);
        billQuery.eq("status", billVo.getStatus());
        if (!StringUtils.isNull(billVo.getOrgId()))
            billQuery.eq("org_id", billVo.getOrgId());
        if (!StringUtils.isNull(billVo.getSupplierId()))
            billQuery.eq("supplier_id", billVo.getSupplierId());
        if (!StringUtils.isNull(billVo.getPumpType()))
            billQuery.eq("pump_type", billVo.getPumpType());
        if (!StringUtils.isNull(billVo.getPumpNorms()))
            billQuery.eq("pump_norms", billVo.getPumpNorms());
        if (!StringUtils.isNull(billVo.getIsBill()))
            billQuery.eq("is_bill", billVo.getIsBill());
        if (!StringUtils.isNull(billVo.getPriceType()))
            billQuery.eq("price_type", billVo.getPriceType());
        if (!StringUtils.isNull(billVo.getStartTime()))
            billQuery.ge("start_time", billVo.getStartTime());
        if (!StringUtils.isNull(billVo.getEndTime()))
            billQuery.lt("start_time", billVo.getEndTime());
        if (!StringUtils.isNull(billVo.getSearchVal()))
            billQuery.and(orderWrapper -> orderWrapper.like("supplier", billVo.getSearchVal()).or()
                    .like("remark", billVo.getSearchVal()));
        IPage iPage = pumpBillDao.selectPage(new Page<>((offset + limit) /limit, limit), billQuery);
        /**分页插件**/
        if (!StringUtils.isNull(iPage.getRecords())) {
            PageInfo pageInfo = new PageInfo(iPage.getRecords());
            if (!StringUtils.isNull(pageInfo.getTotal()))
                iPage.setTotal(pageInfo.getTotal());
        }
        return iPage;
    }

    public List<PumpBillVo> findBillData(PumpBillVo billVo) {
        QueryWrapper<ProductionPumpBill> billQuery = new QueryWrapper<>();
        if (StringUtils.isNull(billVo.getStatus()))
            billVo.setStatus(0);
        billQuery.eq("status", billVo.getStatus());
        if (!StringUtils.isNull(billVo.getOrgId()))
            billQuery.eq("org_id", billVo.getOrgId());
        if (!StringUtils.isNull(billVo.getSupplierId()))
            billQuery.eq("supplier_id", billVo.getSupplierId());
        if (!StringUtils.isNull(billVo.getPumpType()))
            billQuery.eq("pump_type", billVo.getPumpType());
        if (!StringUtils.isNull(billVo.getPumpNorms()))
            billQuery.eq("pump_norms", billVo.getPumpNorms());
        if (!StringUtils.isNull(billVo.getIsBill()))
            billQuery.eq("is_bill", billVo.getIsBill());
        if (!StringUtils.isNull(billVo.getPriceType()))
            billQuery.eq("price_type", billVo.getPriceType());
        if (!StringUtils.isNull(billVo.getToday()))
            billQuery.eq("today", billVo.getToday());
        if (!StringUtils.isNull(billVo.getStartTime()))
            billQuery.ge("start_time", billVo.getStartTime());
        if (!StringUtils.isNull(billVo.getEndTime()))
            billQuery.lt("start_time", billVo.getEndTime());
        billQuery.orderByDesc("supplier_id");
        billQuery.orderByAsc("pump_type", "pump_norms");
        List<ProductionPumpBill> pumpBills = pumpBillDao.selectList(billQuery);
        if (pumpBills == null || pumpBills.size() == 0)
            return null;
        QueryWrapper<PumpType> pumpTypeQuery = new QueryWrapper<>();
        pumpTypeQuery.eq("status", 0);
        List<PumpType> pumpTypeList = pumpTypeDao.selectList(pumpTypeQuery);

        QueryWrapper<PumpNorm> pumpNormsQuery = new QueryWrapper<>();
        pumpNormsQuery.eq("status", 0);
        List<PumpNorm> pumpNormsList = pumpNormDao.selectList(pumpNormsQuery);
        Map<Long, String> pumpTypeMap = new HashMap<>();
        Map<Long, String> pumpNormsMap = new HashMap<>();
        for (PumpType pumpType : pumpTypeList) {
            pumpTypeMap.put(pumpType.getId(), pumpType.getName());
        }
        for (PumpNorm pumpNorm : pumpNormsList) {
            pumpNormsMap.put(pumpNorm.getId(), pumpNorm.getName());
        }

        List<PumpBillVo> resultList = new ArrayList<>();
//        Double tempAvgPrice = 0.0;//平均单价小计
        Double tempSumStere = 0.0;//小计吨位
        Double tempSumPrice = 0.0;//总价
//        Double avgPrice = 0.0;//平均单价
        Double sumStere = 0.0;//总吨位
        Double sumPrice = 0.0;//总价
        Long calcSupplierId = -1l;//泵送公司
        String tempSupplier = "";
        for (int i = 0; i < pumpBills.size(); i++) {
            ProductionPumpBill pumpBill = pumpBills.get(i);
            Long pumpType = StringUtils.convertLong(pumpBill.getPumpType());
            Long pumpNorm = StringUtils.convertLong(pumpBill.getPumpNorms());
            Long supperId = StringUtils.convertLong(pumpBill.getSupplierId());
            String supplier = StringUtils.convertString(pumpBill.getSupplier());
            Double stere = StringUtils.convertDouble(pumpBill.getStere());
            Double tempPrice = StringUtils.convertDouble(pumpBill.getBillMoney());
            if (calcSupplierId.longValue() == -1l) {
                tempSupplier = supplier;
                calcSupplierId = supperId;
            }
            if (i > 0 && !StringUtils.isNull(supperId) && calcSupplierId.longValue() != supperId.longValue()) {//属于另外一种方式
                PumpBillVo tempBillVo = new PumpBillVo();
                tempBillVo.setSupplier(tempSupplier + "<小计>");
                tempBillVo.setStere(StringUtils.formatDouble(tempSumStere, 2));//小计方量
                tempBillVo.setBillMoney(StringUtils.formatDouble(tempSumPrice, 2));//小计结算金额
                if (!StringUtils.isNull(sumStere) && !StringUtils.isNull(tempSumPrice)
                        && tempSumPrice != 0 && sumStere != 0) {
                    tempBillVo.setPrice(StringUtils.formatDouble(tempSumPrice / sumStere , 2));
                } else {
                    tempBillVo.setPrice(0.0);
                }
                tempSumStere = 0.0;
                tempSumPrice = 0.0;
                calcSupplierId = supperId;
                tempSupplier = supplier;
                resultList.add(tempBillVo);
            }
            String pumpTypeName = "";
            String pumpNormsName = "";
            if (!StringUtils.isNull(pumpType))
                pumpTypeName = StringUtils.convertString(pumpTypeMap.get(pumpType));
            if (!StringUtils.isNull(pumpNorm))
                pumpNormsName = StringUtils.convertString(pumpNormsMap.get(pumpNorm));
            PumpBillVo billVo1 = BeanUtil.copyProperties(pumpBill, PumpBillVo.class);
            billVo1.setPumpTypeName(pumpTypeName);
            billVo1.setPumpNormsName(pumpNormsName);
            if (!StringUtils.isNull(pumpBill.getIsBill()) && pumpBill.getIsBill() == 0) {
                tempSumStere += stere;
                tempSumPrice += tempPrice;
                sumPrice += tempPrice;
                sumStere += stere;
            }
            resultList.add(billVo1);
        }
        PumpBillVo tempBillVo = new PumpBillVo();
        tempBillVo.setSupplier(tempSupplier + "<小计>");
        tempBillVo.setStere(StringUtils.formatDouble(tempSumStere, 2));//小计方量
        tempBillVo.setBillMoney(StringUtils.formatDouble(tempSumPrice, 2));//小计结算金额
        if (!StringUtils.isNull(tempSumStere) && !StringUtils.isNull(tempSumPrice)
                && tempSumPrice != 0 && tempSumStere != 0) {
            tempBillVo.setPrice(StringUtils.formatDouble(tempSumPrice / tempSumStere , 2));
        } else {
            tempBillVo.setPrice(0.0);
        }
        resultList.add(tempBillVo);
        tempBillVo = new PumpBillVo();
        tempBillVo.setSupplier("<合计>");
        tempBillVo.setStere(StringUtils.formatDouble(sumStere, 2));//小计方量
        tempBillVo.setBillMoney(StringUtils.formatDouble(sumPrice, 2));//小计结算金额
        if (!StringUtils.isNull(sumStere) && !StringUtils.isNull(sumPrice)
                && sumPrice != 0 && sumStere != 0) {
            tempBillVo.setPrice(StringUtils.formatDouble(sumPrice / sumStere , 2));
        } else {
            tempBillVo.setPrice(0.0);
        }
        resultList.add(tempBillVo);
        if (resultList == null || resultList.size() == 0)
            return null;
        return resultList;
    }
    /**
     *
     * @param billVo
     * @return
     */
    public List<ProductionPumpBill> findBillList(PumpBillVo billVo) {
        QueryWrapper<ProductionPumpBill> billQuery = new QueryWrapper<>();
        if (StringUtils.isNull(billVo.getStatus()))
            billVo.setStatus(0);
        billQuery.eq("status", billVo.getStatus());
        if (!StringUtils.isNull(billVo.getOrgId()))
            billQuery.eq("org_id", billVo.getOrgId());
        if (!StringUtils.isNull(billVo.getSupplierId()))
            billQuery.eq("supplier_id", billVo.getSupplierId());
        if (!StringUtils.isNull(billVo.getPumpType()))
            billQuery.eq("pump_type", billVo.getPumpType());
        if (!StringUtils.isNull(billVo.getPumpNorms()))
            billQuery.eq("pump_norms", billVo.getPumpNorms());
        if (!StringUtils.isNull(billVo.getIsBill()))
            billQuery.eq("is_bill", billVo.getIsBill());
        if (!StringUtils.isNull(billVo.getPriceType()))
            billQuery.eq("price_type", billVo.getPriceType());
        if (!StringUtils.isNull(billVo.getToday()))
            billQuery.eq("today", billVo.getToday());
        if (!StringUtils.isNull(billVo.getStartTime()))
            billQuery.ge("start_time", billVo.getStartTime());
        if (!StringUtils.isNull(billVo.getEndTime()))
            billQuery.lt("start_time", billVo.getEndTime());
        List<ProductionPumpBill> pumpBills = pumpBillDao.selectList(billQuery);
        if (pumpBills == null || pumpBills.size() == 0)
            return null;
        return pumpBills;
    }
    /**
     * 保存结算数据
     * @param bill
     * @return
     */
    public RestResultDto saveBill(ProductionPumpBill bill) {
        if (bill == null)
            throw new MessageCenterException("新增参数错误");
        if (StringUtils.isAllParamNull(bill.getOrgId(), bill.getOrgName()))
            throw new MessageCenterException("请选择公司");
        if (StringUtils.isAllParamNull(bill.getSupplierId(), bill.getSupplier()))
            throw new MessageCenterException("请选择供应商信息");
        if (StringUtils.isNull(bill.getPumpType()))
            throw new MessageCenterException("请选择泵送类型");
        if (StringUtils.isNull(bill.getPumpNorms()))
            throw new MessageCenterException("请选择泵送规格");
        if (StringUtils.isNull(bill.getPriceType()))
            throw new MessageCenterException("请选择价格类型");
        if (StringUtils.isNull(bill.getIsBill()))
            throw new MessageCenterException("请选择结算类型");
        if (StringUtils.isNull(bill.getToday()))
            throw new MessageCenterException("请选择结算月份");
        if (StringUtils.isAllParamNull(bill.getStartTime(), bill.getEndTime()))
            throw new MessageCenterException("请选择结算时间周期");
        if (StringUtils.isNull(bill.getStere()))
            throw new MessageCenterException("请输入结算方量");
        if (StringUtils.isNull(bill.getPrice()))
            throw new MessageCenterException("请输入结算单价");
        QueryWrapper<OaProductionContract> productionQuery = new QueryWrapper<>();
        productionQuery.eq("status", 0);
        productionQuery.eq("type", ProductionContractTypeEnum.PUMP.getCode());
        productionQuery.eq("sid", bill.getSupplierId());
        productionQuery.orderByDesc("id");
        List<OaProductionContract> productionList = productionContractDao.selectList(productionQuery);
        if (productionList == null || productionList.size() == 0)
            throw new MessageCenterException("未获取到该供应商的泵送合同");

        OaProductionContract contract = productionList.get(0);
        Double totalPrice = 0.0;
        if (bill.getPrice().doubleValue() != 0 && bill.getStere().doubleValue() != 0)
            totalPrice = StringUtils.formatDouble(bill.getPrice() * bill.getStere(), 2);
        ProductionPumpBill exBill =  pumpBillDao.findExistBill(bill.getOrgId(), contract.getId(), bill.getSupplierId(),
                bill.getPumpType(), bill.getPumpNorms(), bill.getPriceType(), bill.getToday(), bill.getPrice());
        if (exBill != null) {
            exBill.setPrice(bill.getPrice());
            exBill.setTaxPrice(bill.getPrice());
            exBill.setBillMoney(totalPrice);
            exBill.setTaxBillMoney(totalPrice);
            exBill.setUpdateTime(new Date());
            exBill.setUpdator(bill.getCreator());
            Integer code = pumpBillDao.updateById(bill);
            return RestResultDto.success("更新成功");
        }
        bill.setTaxBillMoney(totalPrice);
        bill.setTaxPrice(bill.getPrice());
        bill.setBillMoney(totalPrice);
        bill.setStatus(0);
        bill.setIsAuto(1);
        bill.setCreateTime(new Date());
        bill.setContractId(contract.getId());
        bill.setIsFinance(0);
        bill.setIsProduction(0);

        Integer code = pumpBillDao.insert(bill);
        if (code > 0)
            return RestResultDto.success("保存成功");
        throw new MessageCenterException("保存失败");
    }

    /**
     * 修改结算数据
     * @param bill
     * @return
     */
    public RestResultDto updateBill(ProductionPumpBill bill) {
        if (bill == null || StringUtils.isNull(bill.getId()))
            throw new MessageCenterException("更新参数错误");
        ProductionPumpBill pumpBill = pumpBillDao.selectById(bill.getId());
        if (pumpBill == null || pumpBill.getStatus() == 1)
            throw new MessageCenterException("更新参数错误");
        bill.setUpdateTime(new Date());
        Integer code = pumpBillDao.updateById(bill);
        if (code > 0)
            return RestResultDto.success("更新成功");
        throw new MessageCenterException("更新失败");
    }

    /**
     * 修改结算数据状态
     * @return
     */
    public RestResultDto updateStatus(Long id, Integer status, String creator) {
        if (StringUtils.isAllParamNull(id, status))
            throw new MessageCenterException("作废参数错误");
        ProductionPumpBill bill = pumpBillDao.selectById(id);
        if (bill == null)
            throw new MessageCenterException("结算记录不存在");
        bill.setUpdator(creator);
        bill.setUpdateTime(new Date());
        bill.setStatus(status);
        Integer code = pumpBillDao.updateById(bill);
        if (code > 0)
            return RestResultDto.success("作废成功");
        throw new MessageCenterException("作废失败");
    }

    /**
     * 修改结算数据状态
     * @return
     */
    public RestResultDto updateStatusV1(String today, Long orgId, Long supplierId, Integer status, String creator) {
        if (StringUtils.isAllParamNull(today, orgId, supplierId, status))
            throw new MessageCenterException("作废参数错误");
        UpdateWrapper<ProductionPumpBill> uw = new UpdateWrapper<>();
        Date nowDate = new Date();
        uw.set("status", status);
        uw.set("update_time", DateUtil.convertString(nowDate));
        uw.set("updator", creator);
        uw.eq("org_id", orgId);
        uw.eq("supplier_id", supplierId);
        uw.eq("today", today);
        Integer code = pumpBillDao.update(new ProductionPumpBill(), uw);
        if (code > 0)
            return RestResultDto.success("作废成功~！");
        throw new MessageCenterException("作废异常");
    }

    /**
     * 修改导出状态
     * @return
     */
    public RestResultDto updateExport(String today, Long orgId, Long supplierId, Integer isExport, String creator) {
        if (StringUtils.isAllParamNull(today, orgId, supplierId, isExport))
            throw new MessageCenterException("更新参数错误");
        UpdateWrapper<ProductionPumpBill> uw = new UpdateWrapper<>();
        Date nowDate = new Date();
        uw.set("is_export", isExport);
        uw.set("update_time", DateUtil.convertString(nowDate));
        uw.set("updator", creator);
        uw.eq("org_id", orgId);
        uw.eq("supplier_id", supplierId);
        uw.eq("is_export", 0);
        uw.eq("today", today);
        Integer code = pumpBillDao.update(new ProductionPumpBill(), uw);
        if (code > 0)
            return RestResultDto.success("更新成功~！");
        throw new MessageCenterException("更新异常");
    }
    /**
     * 查询是否审核
     * @param orgId
     * @param today
     * @return
     */
    public Integer todayExamine(Long orgId, String today) {
        if (StringUtils.isAllParamNull(orgId, today))
            throw new MessageCenterException("查询审核记录参数错误");
        Integer total = pumpBillDao.findExamineTotal(orgId, today);
        return total;
    }
    /**
     * 审核购进明细报表
     * @param orgId
     * @param today 时间点
     * @param creator
     * @param type 0 统计 1 财务
     * @return
     */
    public RestResultDto examineBill(Long orgId, String today, String creator, Integer type) {
        if (StringUtils.isAllParamNull(orgId, today, type))
            throw new MessageCenterException("审核泵送结算表参数错误");

        Date nowDate = new Date();
        String nowTime = DateUtil.convertString(nowDate);

        Integer code = -1;
        if (type == 0) {
            QueryWrapper<ProductionPumpBill> query = new QueryWrapper<>();
            query.eq("status",0);
            query.eq("org_id", orgId);
            query.eq("today", today);
            query.eq("is_production", 0);
            Integer total = pumpBillDao.selectCount(query);
            if (total < 1)
                throw new MessageCenterException("没有审核的数据");
            UpdateWrapper<ProductionPumpBill> uw = new UpdateWrapper<>();
            uw.set("is_production", 1);
            uw.set("updator", creator);
            uw.set("update_time", DateUtil.convertString(nowDate));
            uw.eq("org_id", orgId);
            uw.eq("today", today);
            uw.eq("is_production", 0);
            uw.eq("is_finance", 0);
            pumpBillDao.update(new ProductionPumpBill(), uw);
            return RestResultDto.success("审核成功");
        } else if (type == 1) {
            QueryWrapper<ProductionPumpBill> query = new QueryWrapper<>();
            query.eq("status",0);
            query.eq("org_id", orgId);
            query.eq("today", today);
            query.eq("is_production", 1);
            query.eq("is_finance", 0);
            Integer total = pumpBillDao.selectCount(query);
            if (total < 1)
                throw new MessageCenterException("没有审核的数据");
            UpdateWrapper<ProductionPumpBill> uw = new UpdateWrapper<>();
            uw.set("is_finance", 1);
            uw.set("updator", creator);
            uw.set("update_time", DateUtil.convertString(nowDate));
            uw.eq("org_id", orgId);
            uw.eq("today", today);
            uw.eq("is_production", 1);
            uw.eq("is_finance", 0);
            pumpBillDao.update(new ProductionPumpBill(), uw);
            return RestResultDto.success("审核成功");
        }
        throw new MessageCenterException("审核类型错误");
    }

    /**
     *
     * @param orgId
     * @param supplierId
     * @param pumpNorms
     * @param pumpType
     * @param priceType
     * @param startTime
     * @param endTime
     * @return
     */
    public RestResultDto calcBill(Long orgId, Long supplierId, Long pumpNorms, Long pumpType,
                           Integer priceType, String today, String startTime, String endTime, String creator) {
        if (StringUtils.isAllParamNull(orgId, today, startTime, endTime))
            throw new MessageCenterException("计算泵送结算参数错误");
        String startDate = startTime.substring(0, 10);
        String endDate = endTime.substring(0, 10);
        List<PumpBillVo> billVoList = pumpBillDao.findPumpDayPrice(orgId, supplierId, pumpNorms, pumpType, priceType, startDate, endDate);
        if (billVoList == null || billVoList.size() == 0)
            throw new MessageCenterException("没有结算数据");
        SysOrigin origin = originDao.selectById(orgId);
        if (origin == null)
            throw new MessageCenterException("计算泵送结算公司不存在");
        List<ProductionPumpBill> billList = new ArrayList<>();
        String orgName = origin.getOriginName();
        String fullName = origin.getFullName();
        Date nowDate = new Date();
        for (PumpBillVo billVo : billVoList) {
            Long contractId = billVo.getContractId();
            Long tempSupplierId = billVo.getSupplierId();
            Long tempPumpType = billVo.getPumpType();
            Long tempPumpNorms = billVo.getPumpNorms();
            Integer tempPriceType = billVo.getPriceType();
            String tempSupplier = billVo.getSupplier();
            Double tempPrice = billVo.getPrice();
            Double stere = billVo.getStere();
            Double totalPrice = 0.0;
            if (!StringUtils.isNull(stere) && !StringUtils.isNull(tempPrice) && stere != 0 && tempPrice != 0)
                totalPrice = StringUtils.formatDouble(stere * tempPrice, 2);
            ProductionPumpBill pumpBill = pumpBillDao.findExistBill(orgId, contractId, tempSupplierId,
                    tempPumpType, tempPumpNorms, tempPriceType, today, tempPrice);
            if (pumpBill != null) {
                Integer isAuto = pumpBill.getIsAuto();
                if (!StringUtils.isNull(isAuto) && isAuto == 1)//手动添加的不处理
                    continue;
                pumpBill.setStere(stere);
                pumpBill.setBillMoney(totalPrice);
                pumpBill.setTaxBillMoney(totalPrice);
                pumpBill.setTaxPrice(tempPrice);
                pumpBill.setUpdateTime(nowDate);
                pumpBill.setUpdator(creator);
                pumpBillDao.updateById(pumpBill);
            } else {
                ProductionPumpBill tempBill = new ProductionPumpBill(orgId, orgName, fullName, today, tempSupplierId,
                        tempSupplier, tempPumpType, tempPumpNorms, contractId, tempPriceType,
                        stere, tempPrice, totalPrice, tempPrice, totalPrice,
                        0, -1, startTime, endTime, 0, 0, "", "",
                        0,"", "", 0, 0, "系统计算同步",
                        creator, nowDate, creator, nowDate);
                billList.add(tempBill);
            }
        }
        if (billList == null || billList.size() == 0)
            return RestResultDto.fail("没有结算数据");
        Integer code = pumpBillDao.saveBatch(billList);
        if (code > 0)
            return RestResultDto.success("泵送结算同步成功~！");
        throw new MessageCenterException("泵送结算异常");
    }
    /**
     * 导出明细
     * @param sheetName
     * @param title
     * @param billList
     * @return
     */
    public XSSFWorkbook exportDetail(String sheetName, String title, List<ProductionPumpBill> billList) {
        if (billList == null || billList.size() == 0)
            throw new MessageCenterException("没有任何导出数据~！");
        QueryWrapper<PumpType> typeQuery = new QueryWrapper<>();
        typeQuery.eq("status", 0);
        List<PumpType>  typeList = pumpTypeDao.selectList(typeQuery);
        QueryWrapper<PumpNorm> normsQuery = new QueryWrapper<>();
        normsQuery.eq("status", 0);
        List<PumpNorm>  normList = pumpNormDao.selectList(normsQuery);

        Map<Long, String> typeMap = new HashMap<>();
        Map<Long, String> pumpMap = new HashMap<>();
        Map<Integer, String> priceTypeMap = new HashMap<>();
        if (typeList != null && typeList.size() > 0) {
            for (PumpType pumpType : typeList) {
                typeMap.put(pumpType.getId(), pumpType.getName());
            }
        }
        if (normList != null && normList.size() > 0) {
            for (PumpNorm pumpNorm : normList) {
                pumpMap.put(pumpNorm.getId(), pumpNorm.getName());
            }
        }
        for (int i = 0; i < BillPriceTypeEnum.values().length; i++) {
            priceTypeMap.put(BillPriceTypeEnum.values()[i].getCode(), BillPriceTypeEnum.values()[i].getDescription());
        }

        XSSFWorkbook hw = new XSSFWorkbook();
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < billList.size(); i++) {
            ProductionPumpBill billVo = billList.get(i);
            String billType = "是";
            String isAuto = "自动";
            String isCheck = "已审核";
            String isFinance = "已审核";
            String pumpType = "";
            String pumpNorms = "";
            String priceType = "";
            if (!StringUtils.isNull(billVo.getPumpType()))
                pumpType = StringUtils.convertString(typeMap.get(billVo.getPumpType()));
            if (!StringUtils.isNull(billVo.getPumpNorms()))
                pumpNorms = StringUtils.convertString(pumpMap.get(billVo.getPumpNorms()));
            if (!StringUtils.isNull(billVo.getPriceType()))
                priceType = StringUtils.convertString(priceTypeMap.get(billVo.getPriceType()));
            Map<String, Object> temp = new LinkedHashMap<>();
            if (!StringUtils.isNull(billVo.getIsBill()) && billVo.getPriceType() == 1)
                billType = "否";
            if (!StringUtils.isNull(billVo.getIsAuto()) && billVo.getIsAuto() == 1)
                isAuto = "手动";
            if (!StringUtils.isNull(billVo.getIsProduction()) && billVo.getIsProduction() == 0)
                isCheck = "未审核";
            if (!StringUtils.isNull(billVo.getIsFinance()) && billVo.getIsFinance() == 0)
                isFinance = "未审核";
            temp.put("orgName", billVo.getOrgName());
            temp.put("today", billVo.getToday());
            temp.put("supplier", billVo.getSupplier());
            temp.put("pumpType", pumpType);
            temp.put("pumpNorms", pumpNorms);
            temp.put("priceType", priceType);
            temp.put("stere", StringUtils.formatDouble(billVo.getStere(), 2));
            temp.put("price", StringUtils.formatDouble(billVo.getPrice(), 2));
            temp.put("billMoney", StringUtils.formatDouble(billVo.getBillMoney(), 2));
            temp.put("isBill", billType);
            temp.put("isAuto", isAuto);
            temp.put("startTime", billVo.getStartTime());
            temp.put("endTime", billVo.getEndTime());
            temp.put("isCheck", isCheck);
            temp.put("checker", billVo.getChecker());
            temp.put("checkTime", billVo.getCheckTime());
            temp.put("isFinance", isFinance);
            temp.put("finance", billVo.getFinance());
            temp.put("financeTime", billVo.getFinanceTime());
            temp.put("remark", billVo.getRemark());
            list.add(temp);
        }
        Object[] columns = {"公司","统计月份","泵送公司","泵送类型","泵送规格","费用类型","结算方量(m³)","价格(元)",
                "结算金额(元)","是否结算","生成方式","结算开始时间","结算结束时间","统计审核",
                "审核人","审核时间","财务审核","审核人","审核时间","备注"};
        List<String> keyList = new ArrayList<>();
        keyList.add("stere");
        keyList.add("billMoney");
        hw = ExcelReportUtils.exportTotalReport(sheetName, title, columns,keyList, list, hw);
//        hw = ExcelMainMaterialUtils.materialPriceExamineWorkbook(sheetName, title, columns, list);
        return hw;
    }

    /**
     * 导出结算单
     * @return
     */
    public XSSFWorkbook exportBill(String sheetName, String title, Long orgId, Long supplierId, String startTime,
                                   String endTime, List<ProductionPumpBill> billList, String creator) {
        SysOrigin origin = originDao.selectById(orgId);
        if (origin == null)
            throw new MessageCenterException("承租公司不存在~!");
        OaSupplier oaSupplier = supplierDao.selectById(supplierId);
        if (oaSupplier == null)
            throw new MessageCenterException("出租公司不存在~!");
        List<OaProductionContract> contractList = productionContractDao.findByNameOrType(orgId, supplierId, null, ProductionContractTypeEnum.PUMP.getCode());
        if (contractList == null || contractList.size() == 0)
            throw new MessageCenterException("泵送合同不存在~!");
        OaProductionContract productionContract = contractList.get(0);
        QueryWrapper<PumpType> typeQuery = new QueryWrapper<>();
        typeQuery.eq("status", 0);
        List<PumpType>  typeList = pumpTypeDao.selectList(typeQuery);
        QueryWrapper<PumpNorm> normsQuery = new QueryWrapper<>();
        normsQuery.eq("status", 0);
        List<PumpNorm>  normList = pumpNormDao.selectList(normsQuery);

        Map<Long, String> typeMap = new HashMap<>();
        Map<Long, String> pumpMap = new HashMap<>();
        if (typeList != null && typeList.size() > 0) {
            for (PumpType pumpType : typeList) {
                typeMap.put(pumpType.getId(), pumpType.getName());
            }
        }
        if (normList != null && normList.size() > 0) {
            for (PumpNorm pumpNorm : normList) {
                pumpMap.put(pumpNorm.getId(), pumpNorm.getName());
            }
        }
        title = origin.getFullName() + "结算表";
        XSSFWorkbook xw = ExcelProductionReportUtils.pumpBillWorkbook(sheetName, title, productionContract.getRate(),
                origin.getFullName(), startTime, endTime,
                oaSupplier.getFullName(), typeMap, pumpMap, creator, billList);
        return xw;
    }
}
